package mario;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class StartGame extends JFrame implements KeyListener,Runnable{
    public static void main(String[] args) {
        new StartGame();
        while (true){
            File file = new File("src/music/mario.mp3");
            try {
                Player player = new Player(new FileInputStream(file));
                player.play();
            } catch (FileNotFoundException | JavaLayerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    }

    public StartGame(){
        //窗口大小
        this.setSize(900,600);
        //居中显示
        this.setLocationRelativeTo(null);
        //可见性
        this.setVisible(true);
        //关闭程序
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //大小不可变
        this.setResizable(false);
        //键盘监听器
        this.addKeyListener(this);
        //name
        this.setTitle("超级玛丽尘鸦版");
        StaticValue.init();
        //初始化马里奥
        mario = new Mario(5,20);
        //创建全部场景
        for(int i = 0;i <= 2;i++){
            allBg.add(new BackGround(i));
        }


        //当前场景
        nowBg = allBg.get(1);
        mario.setBackGround(nowBg);

        //绘制图像
        repaint();
        thread.start();
    }

    //存储所有背景
    private List<BackGround> allBg = new ArrayList();
    //存储当前背景
    private BackGround nowBg = new BackGround();
    //双缓存
    private Image offScreenImage = null;
    //定义马里奥对象
    private Mario mario = new Mario();
    //定义线程对象,实现马里奥运动
    private Thread thread= new Thread(this);
    //游戏是否开始
    private boolean isStart = false;



    @Override
    public void paint(Graphics g) {
        //游戏未开始界面
        BufferedImage bufferedImage = new BufferedImage(900, 600, BufferedImage.TYPE_3BYTE_BGR);
        if (offScreenImage == null){
            offScreenImage = createImage(900,600);
        }
        Graphics graphics = offScreenImage.getGraphics();

        if (this.isStart){

            graphics.fillRect(0,0,900,600);
            //绘制背景
            graphics.drawImage(nowBg.getBg(),0,0,this);
            //绘制敌人
            for (Enemy enemy : nowBg.getEnemyList()){
                graphics.drawImage(enemy.getShow(),enemy.getX(),enemy.getY(),this);
            }
            //绘制障碍物
            for (Obstacle obstacle : nowBg.getObstacles()){
                graphics.drawImage(obstacle.getShow(),obstacle.getX(),obstacle.getY(),this);
            }
            //绘制马里奥
            graphics.drawImage(mario.getShow(),mario.getX(),mario.getY(),this);
            //分数
            Color color = graphics.getColor();
            graphics.setColor(color.RED);
            graphics.setFont(new Font("黑体",Font.BOLD,25));
            //graphics.drawString("累计摧毁砖块数:"+mario.getBrick()+"",200,100);
            //graphics.drawString("累计消灭敌人数:"+mario.getMonster()+"",500,100);
            graphics.setColor(color);
        }else {
            graphics.drawImage(StaticValue.start,0,0,this);
        }

        //将图像绘制到窗口
        g.drawImage(offScreenImage,0,0,this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {//w87 a65 s83 d68
        if (e.getKeyCode()==32){
            this.isStart = true;
        }
        if (e.getKeyCode()==65){
            mario.moveLeft();
        }

        if (e.getKeyCode()==68){
            mario.moveRight();
        }

        if (e.getKeyCode()==87){
            mario.jump();
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode()==65){
            mario.stopLeft();
        }

        if (e.getKeyCode()==68){
            mario.stopRight();
        }
    }

    @Override
    public void run() {
        while (true){
            repaint();
            try {
                Thread.sleep(50);
                if(nowBg.getSort()==2){
                    if (Math.abs(mario.getX()-540)<=30 && Math.abs(mario.getY()-170)<=30){
                        mario.setX(800);
                        mario.setY(350);
                    }
                    if(Math.abs(mario.getX()-810)<=10 && Math.abs(mario.getY()-500)<=10){
                        JOptionPane.showMessageDialog(this,"YOU ARE WINNER");
                        System.exit(0);
                    }
                }else {
                    if (mario.getX()>=870) {
                        nowBg = allBg.get(nowBg.getSort()+1);
                        mario.setBackGround(nowBg);
                        mario.setX(5);
                        mario.setY(20);
                    }
                    //nowBg = allBg.get(nowBg.getSort()+1);
                }


                //马里奥死亡
                if (mario.isDeath()){
                    JOptionPane.showMessageDialog(this,"YOU ARE DEAD");
                    mario.setX(5);
                    mario.setY(20);
                    mario.reBirth();
                    //System.exit(0);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
